<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="org.apache.commons.io.FileUtils"%>
<%@page import="cn.imvc.util.AliOss"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.InputStream"%>
<%@page import="com.aliyun.oss.OSSClient"%>
<%@page import="cn.imvc.app.domains.ImageFolder"%>
<%@page import="cn.imvc.app.models.GoodsModel"%>
<%@page import="cn.imvc.app.controllers.goods.GoodsController"%>
<%@page import="java.io.File"%>
<%@page import="cn.imvc.app.commons.SystemConfig"%>
<%@page import="com.baidu.ueditor.ActionEnter"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@ page import="org.springframework.context.ApplicationContext"%>
<%@ page import="org.apache.log4j.Logger" %>
<%

  
    request.setCharacterEncoding( "utf-8" );
	response.setHeader("Content-Type" , "text/html");
	String truePath = "";
	String rootPath = application.getRealPath( "/" );
	Object action = request.getParameter("action");
	ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
	SystemConfig sysConfig = (SystemConfig)ctx.getBean(SystemConfig.class);
	AliOss aliOss = (AliOss)ctx.getBean(AliOss.class);

	String imageFolder = "";
	String tmpFileName = (Math.random() * 10000 + "").replace(".", "");
	String fileType = "";
	SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
	
	if(request.getParameter("imageFolder") != null){
		String imagePath = sysConfig.getImagePath();
		imageFolder = ImageFolder.getByIntValue(Integer.valueOf(request.getParameter("imageFolder").toString())).toString();
		truePath = imagePath+File.separator+imageFolder;
	}
	if(request.getParameter("imageFolder") != null && action != null && "uploadfile".equals(action.toString())){
		String imagePath = sysConfig.getImagePath();
		truePath = imagePath+File.separator;
	}

	DiskFileItemFactory factory=new DiskFileItemFactory();
    ServletFileUpload sfu=new ServletFileUpload(factory);
    Long size = 0L;
    try {
    	System.out.println("a---------------");
        List<FileItem> fileItems= sfu.parseRequest(request);  //解码请求 得到所有表单元素
        System.out.println("b---------------");
        for (FileItem fi : fileItems) {
            //有可能是 文件，也可能是普通文字 
            if (fi.isFormField()) { //这个选项是 文字 
                System.out.println("表单值为："+fi.getString());
            }else{
                // 是文件
                String fn=fi.getName();
                fileType=fn.substring(fn.lastIndexOf("."));
                size = fi.getSize();
                imageFolder+="/"+simpleDateFormat.format(new Date())+"/"+tmpFileName+fileType;
                final String endpoint = aliOss.getEndpoint();
            	final String accessKeyId = aliOss.getAccessKeyId();
            	final String accessKeySecret = aliOss.getAccessKeySecret();

            	// 创建OSSClient实例
            	OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
            	// 上传文件流
            	InputStream inputStream =  fi.getInputStream();
            	ossClient.putObject(aliOss.getBucketName(), imageFolder, inputStream);
            	// 关闭client
            	ossClient.shutdown();
            }                
        }    
        
    } catch (Exception e) {
        e.printStackTrace();
    }

    
    
	String outStr = "";
	if(action != null && "config".equals(action.toString())){
		System.out.println("c---------------");
		outStr = new ActionEnter( request, rootPath, truePath ).exec();
		System.out.println("d---------------");
		//如果是获取请求，更改url前缀
		//outStr = outStr.replace("##imageUrlPrefix##", sysConfig.getImageDomain());
		//System.out.println(outStr);
	}else if(action != null && "uploadimage".equals(action.toString())){
		//如果是上传图片，则更新图片路径
		//String imageFolder = ImageFolder.getByIntValue(Integer.valueOf(request.getParameter("imageFolder").toString())).toString();
		//outStr = outStr.replace("\",\"url\": \"", "\",\"url\": \""+imageFolder.replace( "\\", "/" ));
		outStr = "{\"state\": \"SUCCESS\""
			+",\"size\": \""+size+"\",\"type\": \""+fileType+"\",\"url\": \""+sysConfig.getImageDomain()+imageFolder+"\"}";
	}
	out.write(outStr);
	out.close();

%>